package org.hysoft.spider;

import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.TNonblockingServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TServerSocket;

/**
 * Created by cl on 16/6/11.
 */
public class SpiderNonBlockingService {

    public static final int SERVER_PORT = 8091;
    public  static TProcessor processor;

    public void startServer() {
        try {
            System.out.println("HelloWorld TSimpleServer start ....");

            processor=new  Spider.Processor(new SpiderServiceImpl());


            // 非阻塞 但单线程
            TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(SERVER_PORT);
            TNonblockingServer.Args tArgs = new TNonblockingServer.Args(serverTransport);
            tArgs.processor(processor);
            //tArgs.protocolFactory(new TBinaryProtocol.Factory());
            tArgs.transportFactory(new TFramedTransport.Factory());
            tArgs.protocolFactory(new TCompactProtocol.Factory());
            // tArgs.protocolFactory(new TJSONProtocol.Factory());
            TServer server = new TNonblockingServer(tArgs);
            server.serve();
            System.out.println("HelloWorld TSimpleServer start ok");
        } catch (Exception e) {
            System.out.println("Server start error!!!");
            e.printStackTrace();
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        SpiderNonBlockingService server = new SpiderNonBlockingService();
        server.startServer();
    }

}
